<?php


namespace app\admin\controller;

use  app\admin\model\ProductCateModel;
use think\Db;

class ProductCate extends Base
{
    //列表
    public function index(){

        if(request()->isAjax ()){

            extract(input());

            $map = [];

            if(isset($key)&&$key!=""){

                $map['name'] = ['like',"%" . $key . "%"];

            }

            $field=input('field');//字段

            $order=input('sort');//排序方式

            if($field && $order){

                $od=$field." ".$order;

            }else{

                $od="sort";

            }
            $cate = new ProductCateModel();

            $nowpage = input('get.page') ? input('get.page'):1;

            $limits = input("limit")?input("limit"):10;

            $count = $cate->getAllCount($map);//计算总页面

            $lists = $cate->getAllCate($map, $nowpage, $limits,$od);


            for ($i = 0; $i < count($lists); $i++) {

                $photo = explode(',', $lists[$i]['img']);

                $lists[$i]['img'] = array_values($photo);

            }


            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);

        }

        return view('productcate/index');
    }
    //添加
    public function add_cate(){

        if (request()->isPost()) {

            extract(input());

            $param = input('post.');

            if (!isset($status)) {

                $param['status'] = 1;

            }

            $param['img'] = trim($param['img'], ',');

            $article = new ProductCateModel();

            $flag = $article->insertTo($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

        }
        $this->assign([
            'configState' => config('qiniu_allow_state')
        ]);

        return view('productcate/add');
    }
    //编辑
    public function edit_cate(){

        $cate = new ProductCateModel();

        if(request()->isPost()){

            $param = input('post.');

            $flag = $cate->editCate($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

        }

        $id = input('param.id');

        $this->assign('cate',$cate->getOneCate($id));

        $this->assign([
            'configState' => config('qiniu_allow_state')
        ]);

        return view('productcate/edit');
    }
    //删除
    public function del_cate()

    {

        $id = input('param.id');

        $cate = new ProductCateModel();

        $flag = $cate->delCate($id);

        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

    }

    public function cate_state()

    {

        extract(input());

        $cate = new ProductCateModel();

        $flag = $cate->cateState($id, $num);

        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

    }

    //修改排序
    public function editField(){

        extract(input());

        $res = Db::name($table)->where(['id' => $id ])->setField($field , $value);

        if($res){

            writelog('更新字段成功',200);

            return json(['code' => 200,'data' => '', 'msg' => '更新字段成功']);

        }else{

            writelog('更新字段失败',100);

            return json(['code' => 100,'data' => '', 'msg' => '更新字段失败']);

        }

    }
    //批量删除
    public function batchDelCate(){

        extract(input());

        if(empty($ids)){

            return json(['code'=>100,'msg'=>'请选择要删除的记录！']);

        }

        $cate = new ProductCateModel();

        $flag = $cate->batchDel($ids);

        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);

    }
    //批量禁用
    public function forbiddenCate(){

        extract(input());

        $list = [];

        if($ids){

            $ids = explode(',',$ids);

            for($i=0;$i<count($ids);$i++){

                $param = [

                    'id'=>$ids[$i],

                    'status'=>2

                ];

                $list[] = $param;

            }

        }

        $cate = new ProductCateModel();

        $flag = $cate->forbiddenCate($list);

        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);

    }

    //批量启用
    public function usingCate(){

        extract(input());

        $list = [];

        if($ids){

            $ids = explode(',',$ids);

            for($i=0;$i<count($ids);$i++){

                $param = [

                    'id'=>$ids[$i],

                    'status'=>1

                ];

                $list[] = $param;

            }

        }

        $cate = new ProductCateModel();

        $flag = $cate->usingCate($list);

        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);

    }
}